package pl.olap.main;

import java.io.IOException;

import pl.olap.sites.FDB;

public class FDBRunner {

	public void runSearch() {
		FDB site = new FDB();
		Connector connector = new Connector(site.URL_MAIN);
		String content;
		try {
			content = connector.getContent();
			getDataFromSite(site, content);
			for (int i = 1; i <= 100; i++) {
				System.out.println("iterator " + i);

				connector.connect(site.URL_MAIN + "?page=" + i);
				content = connector.getContent();
				getDataFromSite(site, content);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getDataFromSite(FDB site, String content) {
		String moviesContent = site.find(content,
				"<ol class=\"results\".+ </ol>");
		String[] movies = moviesContent.split("<div class=\"content\">");

		for (int i = 1; i < movies.length; i++) {
			// String movieURL = site.find(movies[i], "<a href=\".+\".+</a>");
			String movieURL = site.findTextBetween(movies[i], "<a href=\"",
					"\".+</a>");
			Connector connector = new Connector(movieURL);
			try {
				String subContent = connector.getContent();
				System.out.println(site.getTitle(subContent));
				System.out.println(site.getYear(subContent));
				site.printArray(site.getGenre(subContent));
				site.printArray(site.getNationality(subContent));
				site.printArray(site.getDirector(subContent));
				System.out.println(site.getDuration(subContent));
				site.printArray(site.getCast(subContent));
				System.out.println(site.getDescription(subContent));
				System.out.println(site.getRating(subContent));
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

}
